/*
工具函数文件
 */
//冒泡排序-从大到小

export const bubbleSort = (arr) => {
	let len = arr.length;
	for (let i = 0; i < len - 1; i++) {
		for (let j = 0; j < len - 1 - i; j++) {
			if (arr[j] < arr[j + 1]) {
				// 相邻元素两两对比
				let temp = arr[j + 1]; // 元素交换
				arr[j + 1] = arr[j];
				arr[j] = temp;
			}
		}
	}
	return arr;
};
export const randomNum = (min, max) => {
	return Math.floor(Math.random() * (max - min) + min);
};
export const randomStr = (e = 16) => {
	let t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678',
		a = t.length,
		n = '';
	for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
	return n;
};
export const getDataType = (data) => {
	let str = Object.prototype.toString.call(data);
	const i = str.indexOf(' ');
	return str.slice(i + 1, -1).toLowerCase();
};
export const isStr = (data) => {
	return getDataType(data) === 'string';
};
export const isNum = (data) => {
	return getDataType(data) === 'number';
};
export const isNaN = (data) => {
	return isNum(data) && isNaN(data);
};
export const isBool = (data) => {
	return getDataType(data) === 'boolean';
};
export const isNull = (data) => {
	return getDataType(data) === 'null';
};
export const isUndefined = (data) => {
	return getDataType(data) === 'undefined';
};
export const isArr = (data) => {
	return getDataType(data) === 'array';
};
export const isObj = (data) => {
	return getDataType(data) === 'object';
};
export const isFun = (data) => {
	return getDataType(data) === 'function';
};

export function deepCopy(target) {
	if (isObj(target) || isArr(target)) {
		const result = isArr(target) ? [] : {};
		for (const key in target) {
			if (isObj(target[key])) {
				result[key] = deepCopy(target[key]);
			} else {
				result[key] = target[key];
			}
		}

		return result;
	}
	return target;
}

//重置表单
export const resetForm = (form) => {
	for (const key in form) {
		const item = form[key];
		if (isObj(item)) {
			resetForm(item);
		} else if (isArr(item)) {
			for (let index = 0; index < item.length; index++) {
				item[index] = null;
			}
		} else {
			form[key] = null;
		}
	}
};

export function isEmpty(value) {
	if (value === '' || value === undefined || value === null) {
		return true;
	}
	return false;
}

export const isValidForm = async (formRef) => {
	return new Promise((resolve, reject) => {
		formRef.value.validate((valid) => {
			if (valid) {
				resolve(true);
			} else {
				reject(false);
			}
		});
	});
};

export function debounce(callback) {
	let timer;
	return function () {
		clearTimeout(timer);
		timer = setTimeout(callback, timeout);
	};
}
export function arrHasData(arr) {
	if (!arr) return false;
	if (arr.length === 0) return false;
	return true;
}

// 笛卡尔积算法
export function descartes(list) {
	//parent上一级索引;count指针计数
	let point = {};

	let result = [];
	let pIndex = null;
	let tempCount = 0;
	let temp = [];

	//根据参数列生成指针对象
	for (let index in list) {
		if (typeof list[index] == 'object') {
			point[index] = { parent: pIndex, count: 0 };
			pIndex = index;
		}
	}

	//单维度数据结构直接返回
	if (pIndex == null) {
		return list;
	}
	const flag = true;
	//动态生成笛卡尔积
	while (flag) {
		let index;
		for (index in list) {
			tempCount = point[index]['count'];
			temp.push(list[index][tempCount]);
		}

		//压入结果数组
		result.push(temp);
		temp = [];

		//检查指针最大值问题
		while (flag) {
			if (point[index]['count'] + 1 >= list[index].length) {
				point[index]['count'] = 0;
				pIndex = point[index]['parent'];
				if (pIndex == null) {
					return result;
				}

				//赋值parent进行再次检查
				index = pIndex;
			} else {
				point[index]['count']++;
				break;
			}
		}
	}
}
//获取主图（如果没有则返回商品图片的第一项）
export const getMainPic = (str) => {
	const picList = JSON.parse(str);
	if (!arrHasData(picList)) return {};
	const mainPic = picList.find(({ isMain }) => isMain);
	return mainPic ? mainPic : picList[0];
};

export const getTreeNodeSum = (tree, childrenKey) => {
	let sum = 0;
	function getSun(tree) {
		tree.forEach((item) => {
			sum++;
			if (item[childrenKey]) {
				getSun(item[childrenKey]);
			}
		});
	}
	getSun(tree);
	return sum;
};

// 去除前后空格
export const removeBilateralSpaces = (text) => {
	let newText = '';
	newText = text ? text.replace(/(^\s*)|(\s*$)/g, '') : '';
	return newText;
};
